#include"DemoSetMassData.h"
#include"LSYEasyBody.h"
#include"LSYEasyWorld.h"
#include"Box2D/Box2D.h"
#include"GLES-Render.h"
#include"Tool.h"

USING_NS_CC;

bool DemoSetMassData::init()
{
	if (!AbstractBox2DTest::init(b2Vec2(0, -10)))
	{
		return false;
	}
	schedule(schedule_selector(DemoSetMassData::work), 10, CC_REPEAT_FOREVER, 1);
	return true;
}

void DemoSetMassData::box2DAppReady()
{
	auto size = Director::getInstance()->getWinSize();
	LSYEasyBody::createRectangle(b2Vec2(0, 0), size.width, size.height);
	pDebug->AppendFlags(b2Draw::e_centerOfMassBit);
	
	createBody();
	
	b2MassData* md = new b2MassData;
	body->GetMassData(md);
	
	md->center.Set(0, -30 * PTM_RADIO);
	md->I = md->mass*md->center.LengthSquared() + 20;
	
	body->SetMassData(md);
	REALSE_NULL(md);
}

void DemoSetMassData::createBody()
{
	body = LSYEasyBody::createCircle(b2Vec2(250, 300), 50);

	b2PolygonShape* triangle = new b2PolygonShape;
	triangle->m_vertices[0] = b2Vec2(-50 * PTM_RADIO, 0);
	triangle->m_vertices[1] = b2Vec2(50 * PTM_RADIO, 0);
	triangle->m_vertices[2] = b2Vec2(0, 150 * PTM_RADIO);
	triangle->m_count = 3;
	
	body->CreateFixture(triangle, 1);
}

void DemoSetMassData::work(float dt)
{
	b2Vec2 impuse;
	impuse.Set(body->GetMass() * 2, 0);
	body->ApplyLinearImpulse(impuse, body->GetWorldPoint(b2Vec2(0, 20 * PTM_RADIO)), true);
}
